<template>
	<div class="container">
		<a-form :form="form">
			<a-form-item label="学期">
				<a-select :allowClear="true" v-decorator="['semesterID',{ rules: [{ required: true, message: '请选择学期' }] }]" placeholder="全部"
					optionFilterProp="children" showSearch>
					<a-select-option v-for="(item, index) in SemesterList" :key="index" :value="item.ID">
						{{ item.Name }}
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="申请类型">
				<a-radio-group @change="Typechang"
					v-decorator="['Type', { rules: [{ required: true, message: '请选择申请类型' }] }]" name="radioGroup">
					<a-radio v-for="(item, i) in TypeData" :key="i" :value="item.Key">
						{{ item.Value }}
					</a-radio>
				</a-radio-group>
			</a-form-item>

			<!-- <a-form-item label="课程类型">
				<a-select :allowClear="true"
					v-decorator="['ActivityTypeID', { rules: [{ required: true, message: '请选择课程类型' }] }]"
					placeholder="全部" optionFilterProp="children" showSearch @change="selectChange">
					<a-select-option v-for="(item, index) in ActivityTypeData" :key="item.ID" :value="item.ID">
						{{ item.Name }}
					</a-select-option>
				</a-select>
			</a-form-item> -->
			<a-form-item label="课程类型">
				<a-tree-select :allowClear="true" show-search
					v-decorator="['ActivityTypeID', { rules: [{ required: true, message: '请选择课程类型' }] }]"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }" :tree-data="ActivityTypeApplyList"
					tree-default-expand-all :replace-fields="replaceFields" placeholder="请先选择课程类型"
					@change="ActivityTypeApplyFun"></a-tree-select>
			</a-form-item>
			<a-form-item label="级别" v-if="Type=='1'">
				<a-select @change="changClass" :allowClear="true"
					v-decorator="['ActivityTypeScoreID', { rules: [{ required: true, message: '请选择级别' }] }]"
					placeholder="全部" optionFilterProp="children" showSearch>
					<a-select-option v-for="(item, index) in ActivityTypeScoreList" :key="item.ID"
						:value="item.ActivityLevelID">
						{{item.ActivityLevelName}}
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="类型" prop="MemberType" v-if="Type=='1'">
				<a-select v-if="zdyApplyCustomScore" v-decorator="['MemberType', { rules: [{ required: true, message: '请选择类型' }] }]"
					style="width:100%">
					<a-select-option v-for="(item, index) in ActivityMemberTypeList" :key="item.Key" :value="item.Key">
						{{ item.Value }}
						<!-- <text
							v-if="!zdyApplyCustomScore">({{item.Key=='1'?jibieDetail.JoinScore:item.Key=='2'?jibieDetail.WorkerScore:item.Key=='3'?jibieDetail.AudienceScore:""}}分)</text> -->
					</a-select-option>
				</a-select>
				<a-select v-else v-decorator="['MemberType', { rules: [{ required: true, message: '请选择类型' }] }]"
					style="width:100%">
					<a-select-option  v-for="(item, index) in ActivityMemberTypeList" :key="item.Key"
						:value="item.Key">{{ item.Value }}
							({{item.Key=='1'?jibieDetail.JoinScore:item.Key=='2'?jibieDetail.WorkerScore:item.Key=='3'?jibieDetail.AudienceScore:""}}分)
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="级别(获奖)" v-if="Type=='2'">
				<a-select @change="levalChange" style="width:100%"
					v-decorator="['ActivityTypePrizeIDssss', { rules: [{ required: true, message: '请选择级别' }] }]">
					<a-select-option v-for="(item, index) in ActivityTypePrizeTypeList" :key="item.Key"
						:value="item.ActivityLevelID">{{ item.ActivityLevelName }}</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="类型(获奖)" v-if="Type=='2'">
				<a-select @change="ActivityTypeFun"
					v-decorator="['ActivityTypePrizeID', { rules: [{ required: true, message: '请选择类型' }] }]"
					style="width:100%">
					<a-select-option v-for="(item, index) in leaveChange" :key="item.Key" :value="item.ID">
						{{ item.Title }} {{item.ApplyCustomScore?'':'（'+item.Score+'分）'}}
						<!-- <text v-if="!item.ApplyCustomScore">({{item.Score}}分)</text> -->
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="分值" v-if="zdyApplyCustomScore">
				<a-input type="number" v-decorator="['Score', { rules: [{ required: true, message: '请输入分值' }] }]" />
			</a-form-item>
			<a-form-item :label="Type=='1'?'活动名称':'奖项名称'">
				<a-input v-decorator="['Title', { rules: [{ required: true, message: '请输入名称' }] }]" />
			</a-form-item>
			<a-form-item label="学号">
				<a-textarea placeholder="请输入学号，多个学号之间请换行" :autosize="{ minRows: 10, maxRows: 20 }"
					v-decorator="['userNo', { rules: [{ required: true, message: '请输入学号' }] }]" />
				当前正在录入第{{ form.getFieldValue('userNo') ? form.getFieldValue('userNo').split('\n').length : 0 }}条数据
			</a-form-item>
			<a-form-item label="是否过滤重复" prop="IsFilter">
				<a-radio-group v-decorator="['IsFilter', { rules: [{ required: true, message: '是否过滤重复' }] }]"
					name="radioGroup">
					<a-radio :value="true">是</a-radio>
					<a-radio :value="false">否</a-radio>
				</a-radio-group>
			</a-form-item>
			<a-form-item :label="Type=='1'?'参加时间':'获奖时间'" prop="GetTime">
				<a-date-picker 
					v-decorator="['GetTimeData', { rules: [{ required: true, message: '请选择时间' }] }]"
					format="YYYY-MM-DD" />
			</a-form-item>

			<!-- 多个 -->
			<a-form-model-item label="佐证材料">
				<a-upload :action="requestUrl+'Common/DoUploadFile?type=file'" :file-list="icofileList"
					:default-file-list="defaultFileList" @change="e => icohandleChange(e, 'Ico')"
					v-decorator="['Ico', { rules: [{ required: true, message: '佐证材料不能为空' }] }]">
					<div>
						<!-- <a-icon type="plus" /> -->
						<!-- <div class="ant-upload-text">Upload</div> -->
						<a-button>
							<a-icon type="upload" /> 上传
						</a-button>
					</div>
				</a-upload>
			</a-form-model-item>
			<a-form-item label="备注">
				<a-textarea placeholder="请输入备注" :autosize="{ minRows: 3, maxRows: 10 }" v-decorator="['Remark']" />
			</a-form-item>
			<a-form-item class="fixed-bottom">
				<a-button type="primary" style="margin-left:400px;" @click="handleSubmit" :loading="loading">申请
				</a-button>
				<a-button style="margin-left: 10px;" @click="reset">重置</a-button>
			</a-form-item>
		</a-form>
	</div>
</template>

<script>
	import moment from 'moment';
	import {
		ManageCreditSendRecordDoAdd,
		ManageCreditFlowTypeDoUpdate,
		CommonDoGetActivityTypeScoreList,
		GetActivityTypePrizeListCommon,
		ManageCreditApplyDoGetDetail,
		GetActivityTypeList,
		CommonDoGetActivityTypePrizeTypeList,
		DoGetActivityMemberTypeListCommon,
		CommonDoGetActivityTypePrizeList,
		CommonDoGetActivityTypeScoreTree,
		CommonDoGetActivityTypePrizeTree,
		DoGetSemesterListCommon
	} from '@/api/follow';
	export default {
		props: {
			currentType: String
		},
		data() {
			return {
				zdyApplyCustomScore: false,
				Type: '1',
				TypeData: [{
						Key: '1',
						Value: "参加活动"
					},
					{
						Key: '2',
						Value: "获奖"
					}
				],
				replaceFields: {
					children: 'children',
					title: 'title',
					key: 'ID',
					value: 'ID'
				},
				SemesterList:[],
				treeselect: true,
				ActivityTypeApplyList: [],
				ActivityTypeApplyID: [],
				itemData: [],
				formLayout: 'horizontal',
				form: this.$form.createForm(this, {
					name: 'coordinated'
				}),
				importIf: false,
				loading: false,
				sendTypeList: this.global.sendTypeList,
				sessionList: [],
				PrizeList: [],
				ActivityTypeData: [],
				ActivityTypePrizeTypeList: [],
				ActivityMemberTypeList: [],
				defaultFileList: [],
				requestUrl: this.global.requestUrl,
				icofileList: [],
				fileList: {},
				showstate: true,
				// ActivityTypePrizeTypeList:[],
				GetActivityTypePrizeListCommon: [],
				ActivityTypeScoreList: [],
				dataForm: [],
				loading: false,
				formLayout: 'horizontal',
				form: this.$form.createForm(this, {
					name: 'coordinated'
				}),
				currentID: '',
				UserList: [],
				jibieDetail: {},
				leaveChange: [],
				ActivityTypePrizeTypeListweiqu: [],
			};
		},
		created() {
			this.getOtherList()
			this.$nextTick(() => {
				this.form.setFieldsValue({
					Type: '1'
				});
			})
		},
		methods: {
			ActivityTypeFun(e) {
				this.zdyApplyCustomScore = this.leaveChange.filter((m, i) => m.ID == e)[0].ApplyCustomScore
			},
			levalChange(e) {
				this.zdyApplyCustomScore = false
				if (e) {
					this.ActivityTypePrizeIDssss = e
					this.leaveChange = this.ActivityTypePrizeTypeList.filter((m, i) => m.ActivityLevelID == e)[0].Data
					console.log(this.leaveChange)
				}
				let that = this;
				if (that.Type == '2') {
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								ActivityTypePrizeID: ''
							});
						}, 300);
					})
				}

			},
			changClass(e) {
				if (e) {
					console.log(e)
					this.ActivityTypeScoreID = e
					this.jibieDetail = this.ActivityTypeScoreList.filter((m, i) => m.ActivityLevelID == e)[0].Data[0]
					this.zdyApplyCustomScore = this.ActivityTypeScoreList.filter((m, i) => m.ActivityLevelID == e)[0].Data[
						0].ApplyCustomScore
					console.log(this.jibieDetail);
				}
				let that = this;
				if (that.Type == '1') {
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								MemberType: ''
							});
						}, 300);
					})
				}


			},
			ActivityTypeApplyFun(e) {
				this.zdyApplyCustomScore = false
				if (e) {
					console.log(e);
					if (this.Type == '1') {
						this.ActivityTypeID = e
						this.ActivityTypeScoreList = this.pubdata.filter((m, i) => m.ID == e)[0].ScoreData;
						console.log(this.ActivityTypeScoreList);
						if(this.ActivityTypeScoreList==undefined){
							this.$message.error('请选择该课程类型下子项课程');
						}

					}
					if (this.Type == '2') {
						this.ActivityTypeID = e
						this.ActivityTypePrizeTypeList = this.pubdata.filter((m, i) => m.ID == e)[0].PrizeData;
						console.log(this.ActivityTypePrizeTypeList);
						if(this.ActivityTypeScoreList==undefined){
							this.$message.error('请选择该课程类型下子项课程');
						}
					}
				}
				let that = this;
				if (that.Type == '1') {
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								ActivityTypeScoreID: '',
								MemberType: ''
							});
						}, 300);
					})
				}
				if (that.Type == '2') {
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								ActivityTypePrizeIDssss: '',
								ActivityTypePrizeID: ''
							});
						}, 300);
					})
				}
			},
			Typechang(e) {
				this.Type = e.target.value;
				this.zdyApplyCustomScore = false
				this.form.resetFields();
				this.MemberType = []
				this.ActivityTypePrizeTypeList = []
				this.ActivityMemberTypeList = []
				this.getOtherList()
				let that = this;
				if (that.Type == '1') {
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								ActivityTypeScoreID: '',
								MemberType: ''
							});
						}, 300);
					})
				}
				if (that.Type == '2') {
					this.$nextTick(() => {
						setTimeout(function() {
							that.form.setFieldsValue({
								ActivityTypePrizeIDssss: '',
								ActivityTypePrizeID: ''
							});
						}, 300);
					})
				}
			},
			toTree(data) {
				let result = [];
				let map = {};
				if (!Array.isArray(data)) {
					return [];
				}
				data.forEach(item => {
					map[item.ID] = item;
				});
				data.forEach(item => {
					let parent = map[item.ParentID];
					if (parent) {
						(parent.children || (parent.children = [])).push(item);
					} else {
						result.push(item);
					}
				});
				return result;
			},
			async getOtherList() {
				let data = {}
				let res3 = await DoGetSemesterListCommon();
				this.SemesterList = res3.data.data;
				data.isAgreeSend = true
				if (this.Type == '1') {
					let res = await CommonDoGetActivityTypeScoreTree(data);
					this.pubdata = res.data.newData
				}
				if (this.Type == '2') {
					let res = await CommonDoGetActivityTypePrizeTree(data)
					this.pubdata = res.data.newData
				}
				this.ActivityTypeApplyList = this.pubdata;
			
				this.ActivityTypeApplyList = this.ActivityTypeApplyList.map(item => {
					return {
						...item,
						title: item.ActivityTypeName
					};
				});
				this.ActivityTypeApplyList = this.toTree(this.ActivityTypeApplyList);
				
				
				
				let res5 = await DoGetActivityMemberTypeListCommon();
				this.ActivityMemberTypeList = res5.data.data;

			},
			icohandleChange({
				fileList
			}, name) {
				this.icofileList = fileList;
			},
			async getDetail(id) {
				let data = {};
				data.id = id
				let res = await ManageCreditApplyDoGetDetail(data);
				this.DetailData = res.data.data;
				let that = this;
				let text = res.data.data;
				console.log(this.DetailData);
				that.dataForm = res.data.formData;

			},
			async getScore(key) {
				let data = {};
				data.activityTypeID = key
				let res4 = await CommonDoGetActivityTypeScoreList(data);
				this.ActivityTypeScoreList = res4.data.data;
				// let arr=res4.data.data;
				// let newArr = arr.filter((currentValue, currentIndex, sourceArr)=>{
				// 	return sourceArr.findIndex(x =>x.LevelID === currentValue.LevelID) === currentIndex
				// });
				// // console.log(newArr);
				// this.ActivityTypeScoreList=newArr
			},
			async getJiang(key) {
				let data = {};
				data.activityTypeID = key
				let res = await CommonDoGetActivityTypePrizeList(data);
				this.ActivityTypePrizeTypeListweiqu = res.data.data
				let arr = res.data.data;
				let newArr = arr.filter((currentValue, currentIndex, sourceArr) => {
					return sourceArr.findIndex(x => x.LevelID === currentValue.LevelID) === currentIndex
				});
				// console.log(newArr);
				this.ActivityTypePrizeTypeList = newArr
			},
			selectChange(e) {
				this.getScore(e);
				this.getJiang(e);
			},
			moment,
			closeFunction(data) {
				this.form.resetFields();
				this.$emit('closeFun', data);
			},
			isAssetTypeAnImage(ext) {
				return ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].
				indexOf(ext.toLowerCase()) !== -1;
			},
			handleSubmit(e) {
				
				this.form.validateFields(async (err, values) => {
					if (!err) {
						let data = {};
						data = values;
						let arr1 = [];
						data.fileData = [];
						if (this.Type == '1') {
							data.ActivityTypeScoreID = this.ActivityTypeScoreList.filter((m, i) => m
								.ActivityLevelID == this.ActivityTypeScoreID)[0].Data[0].ID
							console.log(data);
						}
						for (let i = 0; i < this.icofileList.length; i++) {
							let name = this.icofileList[i].response.fileName;
							let index = name.lastIndexOf(".");
							let ext = name.substr(index + 1);
							let type = ''
							if (this.isAssetTypeAnImage(ext)) {
								type = 1
							} else {
								type = 2
							}
							data.fileData.push({
								FileName: this.icofileList[i].response.fileName,
								FilePath: this.icofileList[i].response.url
							})
						}
						data.attachList = arr1.join('_')
						if (data.GetTimeData != '') {
							// data.GetTime = moment(data.GetTimeData._d).format('YYYY-MM-DD');
							data.GetTime = data.GetTimeData.format('YYYY-MM-DD');
						}
						data.SplitChar = '\n';
						data.userNo = data.userNo.replace(/^\s+|\s+$/g,'');
						let res = {};
						
						if (!this.currentID) {
							res = await ManageCreditSendRecordDoAdd(data);
						} else {
							data.id = this.currentID;
							res = await ManageCreditFlowTypeDoUpdate(data);
						}
						if (res.data.code === 0) {
							this.$message.success(res.data.msg);
							this.$nextTick(() => {
								this.form.resetFields();
								this.$router.push({
									name: 'CreditSendRecord',
								});
							})
							// this.closeFunction('1');
						} else {
							this.$message.error(res.data.msg);
						}
					}
				});
			},
			reset() {
				this.form.resetFields();
				this.importData = []
				this.icofileList=[]
				this.importIf = false
				this.loading = false
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		margin: 20px 50px;
		display: flex;
	}

	/deep/.ant-form-item-label {
		width: 180px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	.ant-form-item {
		margin-bottom: 10px;
	}

	/deep/.ant-form {
		margin-top: 50px;
		padding-bottom: 50px;
	}
</style>
